﻿using System.Web.Mvc;
using ValidationFramework.Web.MVC.Interfaces;

namespace ValidationFramework.Web.MVC
{
	public class ClientValidationEngine
	{
	    readonly IClientValidationGenerator generator;
	    readonly IRuleExtractor extractor;

		/// <summary>
		/// Initializes a new instance of the ClientValidationEngine class.
		/// </summary>
		/// <param name="extractor"></param>
		/// <param name="generator"></param>
		public ClientValidationEngine(IRuleExtractor extractor, IClientValidationGenerator generator)
		{
			this.generator = generator;
			this.extractor = extractor;
		}

		public string GenerateScript(string formId)
		{
			// setup generator and strategy
			var validationScript = generator.Generate(formId , extractor.Extract());
			// build tag
			var scriptTag = new TagBuilder("script");
			scriptTag.Attributes.Add("type", "text/javascript");
			scriptTag.InnerHtml = validationScript;
			return scriptTag.ToString();
		}
	}
}
